package enterprisePerson

import (
	"context"
	"errors"
	"github.com/google/uuid"
	"github.com/spf13/cast"
	"gorm.io/gorm"
	"insighttracking/common/enum"
	"insighttracking/common/utils"
	"insighttracking/query/dao"
	"net/http"

	"insighttracking/apps/admin/internal/svc"
	"insighttracking/apps/admin/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
	"os"
)

type GetEnterprisePersonDetailByIdApiLogic struct {
	logx.Logger
	ctx     context.Context
	svcCtx  *svc.ServiceContext
	Request *http.Request
}

func NewGetEnterprisePersonDetailByIdApiLogic(ctx context.Context, svcCtx *svc.ServiceContext, request *http.Request) *GetEnterprisePersonDetailByIdApiLogic {
	hostname, _ := os.Hostname()
	return &GetEnterprisePersonDetailByIdApiLogic{
		Logger: logx.WithContext(ctx).WithCallerSkip(0).WithFields(
			logx.Field("Log_UUID", uuid.New().String()),
			logx.Field("hostname", hostname)),
		ctx:     ctx,
		svcCtx:  svcCtx,
		Request: request,
	}
}

func (l *GetEnterprisePersonDetailByIdApiLogic) GetEnterprisePersonDetailByIdApi(req *types.IdReq) (resp *types.EnterprisePersonModel, err error) {
	// todo: add your logic here and delete this line
	accountType := cast.ToInt64(l.ctx.Value("accountType"))
	enterpriseId := cast.ToInt64(l.ctx.Value("enterpriseId"))
	if accountType != enum.EnterpriseEnum {
		return &types.EnterprisePersonModel{}, nil
	}
	enterprisePerson, err := dao.EnterprisePersonEntity.WithContext(l.ctx).Where(
		dao.EnterprisePersonEntity.EnterpriseID.Eq(enterpriseId),
		dao.EnterprisePersonEntity.ID.Eq(req.Id),
	).First()
	if !errors.Is(err, gorm.ErrRecordNotFound) {
		return nil, errors.New("传递的主键错误")
	}
	return &types.EnterprisePersonModel{
		ID:           enterprisePerson.ID,                                 // 主键id
		EnterpriseID: enterprisePerson.EnterpriseID,                       // 关联到enterprise表主键id
		Name:         enterprisePerson.Name,                               // 员工姓名
		Mobile:       enterprisePerson.Mobile,                             // 员工手机号码
		Email:        enterprisePerson.Email,                              // 员工电邮地址
		RoleID:       enterprisePerson.RoleID,                             // 角色id,关联到role表主键id
		RoleName:     "",                                                  //  角色名称
		Status:       enterprisePerson.Status,                             // 状态,0表示正常,1表示禁用
		CreatedAt:    utils.TimestampToString(enterprisePerson.CreatedAt), // 创建时间
		UpdatedAt:    utils.TimestampToString(enterprisePerson.UpdatedAt), // 更新时间
	}, nil
}
